package com.abc.controller;

import com.abc.model.TUser;
import com.abc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/create")
    public String createUser(){
        TUser user = new TUser();
        user.setName("cai"+RandomUtils.nextInt(100,1000));
        user.setPwd(new BCryptPasswordEncoder().encode("123456"));
        userService.save(user);
        return "added user: "+user.getName();
    }

    @GetMapping("/me")
    public User me(@AuthenticationPrincipal User u){
        log.info("获取登录用户信息");
        u.eraseCredentials();
        return u;
    }

}
